package com.libcommon

import java.text.SimpleDateFormat
import java.util.Locale

object SimpleDateFormatCache {
    private val SDF_THREAD_LOCAL = object : ThreadLocal<HashMap<String, SimpleDateFormat>>() {
        override fun initialValue(): HashMap<String, SimpleDateFormat> {
            return HashMap()
        }
    }

    fun getSafeDateFormat(pattern: String): SimpleDateFormat {
        val sdfMap = SDF_THREAD_LOCAL.get()
        var simpleDateFormat = sdfMap?.get(pattern)
        if (simpleDateFormat == null) {
            simpleDateFormat = SimpleDateFormat(pattern, Locale.ENGLISH)
            sdfMap?.put(pattern, simpleDateFormat)
        }
        return simpleDateFormat
    }
}